<?php 
class languagesAction extends AdminMaster{
	private $strClass = 0;
	public function execute($request){
		$this->data['navigation']	= __('module_foreignLanguages',null,'menu').' › '. __('module_foreignLanguages_action_languages',null,'menu');
		$this->formData				= $request->getParameter('MasterForeignLanguages');	
		
		$this->data['child']['levels']		= $request->getParameter('levels', array());
		$this->data['child']['level_list']	= MasterForeignLanguageLevelsTable::getDataList(null);
		
		
		switch ($this->data['action']){
			case ADMIN_DEFINED::DELETE_RECORD:
				$this->delRecord();
				break;
			case ADMIN_DEFINED::DISABLE:
				$this->disableRecord();
				break;
			case ADMIN_DEFINED::ENABLE:
				$this->enableRecord();
				break;
			case ADMIN_DEFINED::ADD_RECORD:
				//$this->redirect('/admin/vi/faqs/cates');die;
				$this->addRecord();
				break;
			case ADMIN_DEFINED::EDIT_RECORD:
				//$this->redirect('/admin/vi/faqs/cates');die;
				$this->editRecord();
				break;
			case ADMIN_DEFINED::SAVE_ORDER:
				$this->saveOrder();
				break;
			#MORE ACTION HERE
			default:
				$this->filterList();
				break;
		}

		$this->setVar('data', $this->data);
	}
	private function filterList(){
		$this->setAction();
		$this->data['navigation'].=' › '. __('nav_list',null,'table_list');
		
		$this->data['list']			 		= MasterForeignLanguagesTable::getDataList($this->data['filter'], $this->data['pagination']);
		
		$this->data['pagination']['total']		= MasterForeignLanguagesTable::getDataListCount($this->data['filter']);
		$this->data['pagination']['router']		= 'foreign_language_languages';
		$this->data['pagination']['params']		= $this->data['filter'];
		$pag = new PaginationHelper2($this->data['pagination']);
		$this->data['pagination_pager']	= $pag->getPaginations();
		if($this->data['filter']['filter_key'] == '*'){
			$this->data['filter']['filter_key'] = '';
		}
	}
	private function delRecord(){
		MasterForeignLanguagesTable::delete($this->data['select_list']);
		$this->filterList();
	}
	private function disableRecord(){
		MasterForeignLanguagesTable::disable($this->data['select_list']);
		$this->filterList();
	}
	private function enableRecord(){
		MasterForeignLanguagesTable::enable($this->data['select_list']);
		$this->filterList();
	}
	private function saveOrder(){
		$sortData = array();
		for($i = 0; $i< count($this->data['ids']); $i++){
			$sortData[$this->data['ids'][$i]] = $this->data['sort_orders'][$i];
		}
		MasterForeignLanguages::saveSortOrder($sortData);
		$this->filterList();
	}
	/*********************************************************************************
	*
	* ADD NEW
	* 
	**********************************************************************************/
	private function addRecord(){
		$this->data['navigation'].=' › '. __('nav_add_new',null,'table_list');
		$this->data['button_action'] = __('button_add',null,'table_list');
		$this->setTemplate('languagesForm');
		
		$form = new MasterForeignLanguagesFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$product_id = MasterForeignLanguagesTable::insert($this->formData);
				if($product_id > 0){
					$this->data['result']	= __('add_new_success', null, 'foreign_language_languages');
					
					$this->addLevels($product_id);
					$this->data['child']['levels']	= array();
						
					$form = new MasterForeignLanguagesFForm();
						
				}else{
					$this->data['result']	= sprintf(__('add_new_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
					
			}
		}
		$this->setVar('form', $form);
	}

	/*
	 * EDIT
	*/
	private function editRecord(){
		$this->data['navigation'].=' › '. __('nav_edit',null,'table_list');
		$this->data['action'].='&id='.$this->data['id'];
		$this->data['button_action'] = __('button_edit',null,'table_list');
		$this->setTemplate('languagesForm');

		$form = new MasterForeignLanguagesFForm();
		if($this->data['is_post']){
			$form->bind($this->formData);
			if($form->isValid()){
				$this->formData['id'] = $this->data['id'];
				if(MasterForeignLanguagesTable::update($this->formData)){
					$this->data['result']	= __('edit_success', null, 'foreign_language_languages');
					$this->addLevels($this->data['id']);
						
					$this->data['child']['levels'] = array();
					$this->data['child']['levels'] = ForeignLanguageToLevelsTable::getListByLanguageId($this->data['id']);
					$arrTmp = array();
					foreach($this->data['child']['levels'] as $val){
						$arrTmp[$val['foreign_language_level_id']] = 'checked';
					}
					$this->data['child']['levels']	= $arrTmp;
				}else{
					$this->data['result']	= sprintf(__('edit_error', null, 'table_list'), sfConfig::get('app_email_admin')) ;
				}
				
			}
		}else{
			$data = MasterForeignLanguagesTable::getDataByIdAdmin($this->data['id']);
			if(count($data)<0){
				$this->redirect($this->data['redirect']);
			}else{
				$this->formData = array(
						'language_name'				=> $data['language_name'],
						'description'		=> $data['description'],
						'is_enabled'		=> $data['is_enabled'],
						'_csrf_token'=> $form->getCSRFToken()
				);
				$form->bind($this->formData);
				$this->data['child']['levels'] = array();
				$this->data['child']['levels'] = ForeignLanguageToLevelsTable::getListByLanguageId($this->data['id']);
				$arrTmp = array();
				foreach($this->data['child']['levels'] as $val){
					$arrTmp[$val['foreign_language_level_id']] = 'checked';
				}
				$this->data['child']['levels']	= $arrTmp;
			}
		}
		$this->setVar('form', $form);
		
	}
	
	private function addLevels($language_id = 0){
		$language_id = intval($language_id);
		if($language_id <=0){
			return;
		}
	
		ForeignLanguageToLevelsTable::deleteByLanguageId($language_id);
		foreach($this->data['child']['levels'] as $val){
			$ptData = array(
					'foreign_language_id'		=> $language_id,
					'foreign_language_level_id'	=> $val
			);
			ForeignLanguageToLevelsTable::insert($ptData);
	
		}
	}
	
	#ACTION FOR EACH ROW
	private function setAction(){
		$action = array();
		$action[]=array(
				'href'	=>	'?action_name='.ADMIN_DEFINED::EDIT_RECORD.'&id=%s',
				'text'	=>	__('action_edit',null,'table_list'),
				'title' =>	__('action_edit',null,'table_list'),
		);
	
		$this->data['action_list']	= $action;
	}
}